/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.jet.JETEmitter;

import edu.pku.sei.modeler.codegen.Activator;
import edu.pku.sei.modeler.jet.util.ModelerConstants;
import edu.pku.sei.modeler.model.core.MetaClass;

/**
 * @author zl
 *
 */
public class GenEditParts extends AbstractGenerator {
	private List blocksToGen;
	private List linksToGen;
	private IFolder folder;
	
	public void setBlocksToGen(List blocksToGen) {
		this.blocksToGen = blocksToGen;
	}
	
	public void setLinksToGen(List linksToGen) {
		this.linksToGen = linksToGen;
	}
	@Override
	public void generateCode(IProgressMonitor monitor) throws InterruptedException {
		assert blocksToGen != null;
		assert linksToGen != null;
		assert proj != null;
		String codeBase = ModelerConstants.pkgBase + "." + ModelerConstants.editpartBase;
		
		folder = proj.getFolder("src/"+codeBase.replace('.', '/'));
		try {
			folder.create(true, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("create editpart folder failed");
		}
		
		genFactory(monitor);
		genBlockEditParts(monitor);
		genTreeEditParts(monitor);
		genLinkEditParts(monitor);
	}
	
	private translated.LinkEditPartGen genLinkEditPart = translated.LinkEditPartGen.create("\n");
	private void genLinkEditParts(IProgressMonitor monitor) throws InterruptedException {
		try {
	/*		String pluginId = Activator.getPluginId();
			String relativeUri = "templates/editparts/LinkEditPart.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.model.notation");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
	*/		
			for(int i=0; i<linksToGen.size(); i++) {
				if(!(linksToGen.get(i) instanceof MetaClass))continue;
				//String result = emitter.generate(monitor, new Object[]{linksToGen.get(i)});
				MetaClass cls = (MetaClass)linksToGen.get(i);
				String result = genLinkEditPart.generate(cls);
				
				IFile javaFile = folder.getFile(cls.getName()+"Editpart.java");
				
				ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
				javaFile.create(is, true, monitor);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate link editpart code failed");
		}
	}
	
	private translated.NodeEditPartGen genNodeEditPart = translated.NodeEditPartGen.create("\n");
	private void genBlockEditParts(IProgressMonitor monitor) throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/editparts/NodeEditPart.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.model.notation");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
			for(int i=0; i<blocksToGen.size(); i++) {
				if(!(blocksToGen.get(i) instanceof MetaClass))continue;
				MetaClass cls = (MetaClass)blocksToGen.get(i);
				//String result = emitter.generate(monitor, new Object[]{blocksToGen.get(i)});
				String result = genNodeEditPart.generate(cls);
				
				IFile javaFile = folder.getFile(cls.getName()+"Editpart.java");
				
				ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
				javaFile.create(is, true, monitor);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate editpart code failed");
		}
	}
	
	private translated.TreeEditPartGen genTreeEditPart = translated.TreeEditPartGen.create("\n");
	private void genTreeEditParts(IProgressMonitor monitor) throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/editparts/TreeEditPart.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.model.notation");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
			for(int i=0; i<blocksToGen.size(); i++) {
				if(!(blocksToGen.get(i) instanceof MetaClass))continue;
				MetaClass cls = (MetaClass)blocksToGen.get(i);
				//String result = emitter.generate(monitor, new Object[]{blocksToGen.get(i)});
				String result = genTreeEditPart.generate(cls);
				
				IFile javaFile = folder.getFile(cls.getName()+"TreeEditpart.java");
				
				ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
				javaFile.create(is, true, monitor);
			}
			for(int i=0; i<linksToGen.size(); i++) {
				if(!(linksToGen.get(i) instanceof MetaClass))continue;
				//String result = emitter.generate(monitor, new Object[]{linksToGen.get(i)});
				MetaClass cls = (MetaClass)linksToGen.get(i);
				String result = genTreeEditPart.generate(cls);
				
				IFile javaFile = folder.getFile(cls.getName()+"TreeEditpart.java");
				
				ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
				javaFile.create(is, true, monitor);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate tree editpart code failed");
		}
	}
	
	
	private translated.EditpartFactoryGen genEditpartFactory = translated.EditpartFactoryGen.create("\n");
	private translated.EpTreeFactoryGen genEpTreeFactory = translated.EpTreeFactoryGen.create("\n");
	private void genFactory(IProgressMonitor monitor) throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/editparts/EditPartFactory.javajet";
*/			
			ArrayList modelList = new ArrayList();
			modelList.addAll(blocksToGen);
			modelList.addAll(linksToGen);
			
			// generate editpart factory
/*			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.model.notation");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
			//String result = emitter.generate(monitor, new Object[]{modelList});
			String result = genEditpartFactory.generate(modelList);
			IFile javaFile = folder.getFile("ModelEditpartFactory.java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
			
			// generate tree editpart factory
/*			relativeUri = "templates/editparts/TreeEditPartFactory.javajet";
			emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
			
			result = emitter.generate(monitor, new Object[]{modelList});
 */
			result = genEpTreeFactory.generate(modelList);
			javaFile = folder.getFile("ModelTreeEditpartFactory.java");
			is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate editpart factory failed");
		}
	}
}
